<template>
  <div class="permission">
    <el-table :data="MeunData" border style="width: 100%" row-key="id">
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column prop="code" label="权限值" width="180" />
      <el-table-column prop="updateTime" label="修改时间" />
      <el-table-column label="操作">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            @click="handleClick(row)"
            size="small"
            :disabled="row.level == 4 ? true : false"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="updatePermisstion(row)"
            :disabled="row.level == 1 ? true : false"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`你确定要删除${row.name}?`"
            width="260px"
            @confirm="removeMenu(row.id)"
          >
            <template #reference>
              <el-button
                type="danger"
                size="small"
                :disabled="row.level == 1 ? true : false"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      v-model="dialogVisible"
      :title="MeunParamsData.id ? '编辑菜单' : '添加菜单'"
      width="500"
      :before-close="handleClose"
    >
      <el-form>
        <el-form-item label="名称">
          <el-input
            placeholder="请你输入菜单名称"
            v-model="MeunParamsData.name"
          />
        </el-form-item>
        <el-form-item label="权限">
          <el-input
            placeholder="请你输入权限名称"
            v-model="MeunParamsData.code"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import {
  MenuParams,
  PermisstionResponseData,
  Permisstion,
} from '@/api/acl/menu/type'
import { reqAllPermission, reqMenuAddOrUpdate, reqRemove } from '@/api/acl/menu'
import { ElMessage } from 'element-plus'
const MeunData = ref<any>([1, 2])
const dialogVisible = ref(false)
const MeunParamsData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})
onMounted(() => {
  getHasPermission()
})
const getHasPermission = async () => {
  let result: PermisstionResponseData = await reqAllPermission()
  if (result.code == 200) {
    MeunData.value = result.data
  }
}
const handleClose = () => {
  dialogVisible.value = false
}
const handleClick = (row: Permisstion) => {
  Object.assign(MeunParamsData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  dialogVisible.value = true
  MeunParamsData.level = (row.level + 1) as number
  MeunParamsData.pid = row.id as number
}
const updatePermisstion = (row: Permisstion) => {
  dialogVisible.value = true
  Object.assign(MeunParamsData, row)
}
const save = async () => {
  let result: any = await reqMenuAddOrUpdate(MeunParamsData)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: MeunParamsData.id ? '修改成功' : '添加成功',
    })
    getHasPermission()
  }
}
const removeMenu = async (id: number) => {
  let result: any = await reqRemove(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getHasPermission()
  }
}
</script>
